/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VisualClien.java
 *
 * Created on 05.01.2012, 22:08:23
 */
package appClient;

import engine.AdressBooks;
import engine.Singleton;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTable;
import org.json.simple.parser.ParseException;
import photo.ReadFile;
import server.MultiThreadedServer;


/**
 *
 * @author User
 */
public class VisualClien extends javax.swing.JFrame {
   private static ConnectClient cli;
   public JTable TableContact;

    
  /** Creates new form VisualClien */
  public VisualClien() {
    initComponents();
    MultiThreadedServer ms = new MultiThreadedServer();
    ms.start();
   // new UserComman(ScrollList, FieldPannel);
    Singleton.getInstance();
  }
  
  public void setClient(ConnectClient cli){
     this.cli = cli;
     cli.setVisual(this);
  }
  
  public static ConnectClient getCli() {
        return cli;
   }
 

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        UserInterface = new javax.swing.JPanel();
        ConnectionButton = new javax.swing.JButton();
        LoginLable = new javax.swing.JLabel();
        LoginField = new javax.swing.JTextField();
        PassLable = new javax.swing.JLabel();
        PassField = new javax.swing.JTextField();
        ButtonGetList = new javax.swing.JButton();
        ButtonInfo = new javax.swing.JButton();
        ButtonNew = new javax.swing.JButton();
        ButtonAddInfo = new javax.swing.JButton();
        FieldPannel = new javax.swing.JPanel();
        ScrollContact = new javax.swing.JScrollPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        UserInterface.setBackground(new java.awt.Color(255, 255, 255));

        ConnectionButton.setText("LogIn");
        ConnectionButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ConnectionButtonActionPerformed(evt);
            }
        });

        LoginLable.setText("Login");

        LoginField.setText("Admin");

        PassLable.setText("Pass");

        PassField.setText("Admin");

        ButtonGetList.setText("GetList");
        ButtonGetList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonGetListActionPerformed(evt);
            }
        });

        ButtonInfo.setText("Info");
        ButtonInfo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonInfoActionPerformed(evt);
            }
        });

        ButtonNew.setText("New");
        ButtonNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonNewActionPerformed(evt);
            }
        });

        ButtonAddInfo.setText("Add Info");
        ButtonAddInfo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonAddInfoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout UserInterfaceLayout = new javax.swing.GroupLayout(UserInterface);
        UserInterface.setLayout(UserInterfaceLayout);
        UserInterfaceLayout.setHorizontalGroup(
            UserInterfaceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(UserInterfaceLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(UserInterfaceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, UserInterfaceLayout.createSequentialGroup()
                        .addComponent(LoginLable, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(LoginField, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, UserInterfaceLayout.createSequentialGroup()
                        .addComponent(PassLable, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(PassField, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, UserInterfaceLayout.createSequentialGroup()
                        .addGroup(UserInterfaceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(ButtonGetList)
                            .addComponent(ConnectionButton, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(ButtonInfo)
                            .addComponent(ButtonNew)
                            .addComponent(ButtonAddInfo))
                        .addGap(22, 22, 22))))
        );
        UserInterfaceLayout.setVerticalGroup(
            UserInterfaceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(UserInterfaceLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(UserInterfaceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LoginField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(LoginLable))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(UserInterfaceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PassField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(PassLable))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ConnectionButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ButtonGetList)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ButtonInfo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ButtonNew)
                .addGap(60, 60, 60)
                .addComponent(ButtonAddInfo)
                .addContainerGap(113, Short.MAX_VALUE))
        );

        FieldPannel.setBackground(new java.awt.Color(204, 204, 204));
        FieldPannel.setMaximumSize(new java.awt.Dimension(300, 300));
        FieldPannel.setMinimumSize(new java.awt.Dimension(300, 300));
        FieldPannel.setPreferredSize(new java.awt.Dimension(300, 300));

        javax.swing.GroupLayout FieldPannelLayout = new javax.swing.GroupLayout(FieldPannel);
        FieldPannel.setLayout(FieldPannelLayout);
        FieldPannelLayout.setHorizontalGroup(
            FieldPannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(ScrollContact, javax.swing.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE)
        );
        FieldPannelLayout.setVerticalGroup(
            FieldPannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(ScrollContact, javax.swing.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(FieldPannel, javax.swing.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(UserInterface, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(UserInterface, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(FieldPannel, javax.swing.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

  private void ConnectionButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ConnectionButtonActionPerformed
    try {
      UserComman.connection(LoginField.getText(), PassField.getText(), this);
    } catch (IOException ex) {
      Logger.getLogger(VisualClien.class.getName()).log(Level.SEVERE, null, ex);
    }
  }//GEN-LAST:event_ConnectionButtonActionPerformed

private void ButtonGetListActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonGetListActionPerformed
    UserComman.getList(cli);
}//GEN-LAST:event_ButtonGetListActionPerformed

private void ButtonInfoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonInfoActionPerformed
    int rowIndex = TableContact.getSelectedRow();
    String id = (String) TableContact.getValueAt(rowIndex, 0);
    UserComman.getInfo(cli, id);
}//GEN-LAST:event_ButtonInfoActionPerformed

  private void ButtonNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonNewActionPerformed
   // new AdressBooks(cli).setVisible(true);
    ClientCommand.loadTypes(cli.clientProtocol, cli);
  }//GEN-LAST:event_ButtonNewActionPerformed

  private void ButtonAddInfoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonAddInfoActionPerformed
    new AddInfo(cli).setVisible(true);
  }//GEN-LAST:event_ButtonAddInfoActionPerformed

  public void makeInvisible()
  {
    ButtonAddInfo.setVisible(false);
  }
  
  public void makeVisible()
  {
    ButtonAddInfo.setVisible(true);
  }
  /**
   * @param args the command line arguments
   */
  public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
          javax.swing.UIManager.setLookAndFeel(info.getClassName());
          break;
        }
      }
    } catch (ClassNotFoundException ex) {
      java.util.logging.Logger.getLogger(VisualClien.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
      java.util.logging.Logger.getLogger(VisualClien.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
      java.util.logging.Logger.getLogger(VisualClien.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
      java.util.logging.Logger.getLogger(VisualClien.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {

      public void run() {
        new VisualClien().setVisible(true);
      }
    });
  }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ButtonAddInfo;
    private javax.swing.JButton ButtonGetList;
    private javax.swing.JButton ButtonInfo;
    private javax.swing.JButton ButtonNew;
    private javax.swing.JButton ConnectionButton;
    private javax.swing.JPanel FieldPannel;
    private javax.swing.JTextField LoginField;
    private javax.swing.JLabel LoginLable;
    private javax.swing.JTextField PassField;
    private javax.swing.JLabel PassLable;
    public javax.swing.JScrollPane ScrollContact;
    private javax.swing.JPanel UserInterface;
    // End of variables declaration//GEN-END:variables

    
}
